clear all 
set more off

* This code creates Figure 3
* wage exits by earning deciles


* set up graph colors
set autotabgraphs on

local msize1 "medsmall"
local msize2 "medlarge"

local color1 "0 76 167"
local color2 "146 0 0"
local color3 "62 150 81"
local colorgb "white"

local color1b "0 76 167"
local color2b "146 0 0"
local color3b "62 150 81"

* load
use "Exits_EarningDeciles.dta", clear 


// regression line for each earnings bin
local x "M"

gen slope_`x' = .
gen u95_`x' = .
gen d95_`x' = .

forvalues ii = 1/10 {

	sum ytrate if trans == "W`ii'`x'"

	if `r(N)' > 2 {
		reg ytrate lcgdp if trans == "W`ii'`x'", cluster(ccode)
		predict _hold

		replace slope_`x' = _b[lcgdp] if trans == "W`ii'`x'"
		replace d95_`x' = _b[lcgdp] - invttail(e(df_r),0.025)*_se[lcgdp] if trans == "W`ii'`x'"
		replace u95_`x' = _b[lcgdp] + invttail(e(df_r),0.025)*_se[lcgdp] if trans == "W`ii'`x'"

		drop _hold
	}
}



sort ccode year earnbin
bys ccode: gen counter = _n
replace counter =. if counter > 10
replace counter = 1 if !missing(counter)


#delimit ;
twoway
(rcap d95_M u95_M earnbin, lcolor("`color1'*.5"))
(scatter slope_M earnbin, mcolor("`color1'"))
if counter == 1 & ccode == "CHE",
name(connect_slope_M)
legend(off)
graphregion(color("`colorgb'")) xtitle("Earnings Deciles") yscale(r(-0.15 0.05)) ylabel(-0.15(0.05)0.05)
xscale(r(1 10)) xlabel(1(1)10)
ytitle("");
#delimit cr	

graph export "Figures/Figure3A.tif", replace 
graph export "Figures/pdf_figures/Figure3A.pdf", replace 


bys ccode trans: egen mean_ytrate = mean(ytrate)	
sort ccode counter earnbin

#delimit ;
twoway
(connected mean_ytrate earnbin if ccode == "CHE", lcolor("`color1'") mcolor("`color1'") msymbol(O))
(connected mean_ytrate earnbin if ccode == "PER", lcolor("`color2'") mcolor("`color2'") msymbol(Dh))
(connected mean_ytrate earnbin if ccode == "BOL", lcolor("`color3'") mcolor("`color3'") lpattern(dash) msymbol(Oh)) 
(connected mean_ytrate earnbin if ccode == "GBR", lcolor(black) mcolor(black) lpattern(dash) msymbol(D)) 	
if counter == 1,
name(connect_T_WM)
legend(order(1 4 3 2) col(4) label(1 "CHE") label(2 "PER") label(3 "BOL") label(4 "GBR"))
graphregion(color("`colorgb'")) xtitle("Earnings Deciles") yscale(r(0 0.4)) ylabel(0(.1)0.4)
ytitle("")
xscale(r(1 10)) xlabel(1(1)10);
graph export "Figures/Figure3B.tif", replace ;
graph export "Figures/pdf_figures/Figure3B.pdf", replace ;

#delimit cr			


